package com.jiandan.utils;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.Log;

/**
 * 工具类
 * 
 * @author hewei
 * 
 */
public class Tools {

    private static Dialog dialog;

    /**
     * 显示加载进度条
     * 
     * @param context
     * @param msg 进度条显示信息
     * @return
     */
    public static void buildLoadingDialog(Context context) {

        dialog = new ProgressDialog(context);

        ((ProgressDialog) dialog).setIndeterminate(true);

        ((ProgressDialog) dialog).setProgressStyle(ProgressDialog.STYLE_SPINNER);

        ((ProgressDialog) dialog).setMessage("正在努力煎蛋，请稍后...");

        // dialog.setCancelable(isCancelable);

        dialog.show();
    }

    public static void progressDialogDissmiss() {

        if (dialog != null && dialog.isShowing()) {

            dialog.dismiss();

        }
    }

    /**
     * 检查网络
     * 
     * @param context
     * @return
     */
    public static boolean isConnect(Context context) {

        try {

            ConnectivityManager connectivity = (ConnectivityManager) context

            .getSystemService(Context.CONNECTIVITY_SERVICE);

            if (connectivity != null) {

                NetworkInfo info = connectivity.getActiveNetworkInfo();

                if (info != null && info.isConnected()) {

                    if (info.getState() == NetworkInfo.State.CONNECTED) {

                        return true;

                    }

                }

            }

        } catch (Exception e) {

            Log.v("error", e.toString());

        }

        return false;

    }

    /**
     * 获取当前程序的版本号
     */
    public static int getVersionCode(Context context) throws NameNotFoundException {

        PackageManager packageManager = context.getPackageManager();

        PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);

        return packInfo.versionCode;

    }

    /**
     * 通过浏览器下载app文件
     * 
     * @param url
     */
    public static void loadAppFromBrowser(Context context, String url) {

        Intent intent = new Intent();

        intent.setAction("android.intent.action.VIEW");

        Uri content_url = Uri.parse(url);

        intent.setData(content_url);

        intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");

        context.startActivity(intent);

    }

}
